<?php

	/**
	 * @see Bike_Model_Mapper_Abstract
	 */
	require_once 'Bike/Model/Mapper/Abstract.php';
	
	/**
	 * @see Bike_Model_Mapper_Dependence
	 */
	require_once 'Bike/Model/Mapper/Dependence.php';
	
	
	class Bike_Model_Mapper_Module extends Bike_Model_Mapper_Abstract
	{
		const DB_TABLE_NAME = 'module';
		const DB_TABLE_PK_NAME = 'module_id';
		const DB_TABLE_ALIAS = 'm';
		const DB_TABLE_LOCALIZATION = True;

		static public function getInstance()
	    {
	        return self::_getInstance(__CLASS__);
	    }

	    protected function _getDependences()
	    {	    	return array(
	    		$this->createDependence(array(
					'type' => Bike_Model_Mapper_Dependence::JOIN_LEFT,
					'name' => 'status',
					'mapperClass' => 'Bike_Model_Mapper_Status',
					'parentJoinFields' => array('status_id'),
				)),
	    	);
	    }
	    
	    public function getDefaultValues()
	    {
	    	return array(
		    	'status_id' => $this->getStatusIdActive(),
		    	'date_install' => time(),
	    	);
	    }
	    
	    public function getWindowTitleEdit(Bike_Model_Abstract $model)
	    {
	    	return $model->getTitle();
	    }
	    
	    public function getModuleIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Module ID'),
		    		'order' => 10,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getNameFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Module name'),
		    		'order' => 20,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getTitleFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Title'),
		    		'order' => 30,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
	    			'required' => true,
					'validators' => array(
						array('StringLength', False, array('min' => '4')),
					),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getAdminMenuTitleFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Admin menu title'),
		    		'order' => 40,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
	    			'required' => true,
					'validators' => array(
						array('StringLength', False, array('min' => '4')),
					),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getNamespaceFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Module namespace'),
		    		'order' => 50,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'description' => $this->translate('Namespace of module. Example: Bike_Module_MyModule_'),
		    		'required' => true,
					'validators' => array(
						array('Regex', False, array('pattern' => '/^Bike_Module_[\w\d]+\_$/')),
					),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getFolderNameFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
	    			'label' => $this->translate('Folder name'),
	    			'order' => 60,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
	    			'description' => $this->translate('Name of folder in file system. Example: my-module'),
		    		'required' => true,
					'validators' => array(
						array('Regex', False, array('pattern' => '/^[-a-z\d]+$/')),
					),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getConfigFileNameFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Config file name'),
		    		'order' => 70,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
	    			'description' => $this->translate('Name of configuration file. Example: config.ini'),
		    		'required' => true,
					'validators' => array(
						array('Regex', False, array('pattern' => '/^[\w\d\_]+\.(ini|php|xml)$/')),
					),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getDateInstallFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Date of install'),	
		    		'order' => 80,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'type' => 'date',
			    	'disabled' => true,
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
	    			'xtype' => 'datecolumn',
					'format' => 'd.m.Y',
	    		);
	    		case self::PARAMS_LIST_MODEL: return array(
		    		'type' => 'date',
					'dateFormat' => 'timestamp',
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getStatusIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Status'),
		    		'order' => 90,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'type' => 'radio',
		    		'required' => false,
		    		'multiOptions' => Bike_Model_Mapper_Status::getInstance()->fetchAll()->getMultiOptions('status_id', 'title'),
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
	    			'dataIndex' => $this->getLocalizedFieldName('status_title'),
	    		);
	    		default: return array();
	    	}
	    }
	    
	}